package com.lyz.dataStructure.LeetCode.dongtaiguihua;

/*
leetcode 213 打家劫舍2

 */
public class Solution1 {

    public static void main(String[] args) {
        int[] arr = {2,7,9,3,1};
        System.out.println(rob(arr));
    }

    public static int rob(int[] nums){
        if(nums==null||nums.length==0){
            return 0;
        }
        int n =nums.length;
        if(n==1){
            return  nums[0];
        }

        return Math.max(rob1(nums,0,n-2),rob1(nums,1,n-1));
    }

    public static int rob1(int[] arr,int start,int end){
        int a =0,b=0,c;
        for(int i =start;i<=end;i++){
            c = Math.max(a+arr[i],b);
            a =b;
            b =c;
        }
        return b;
    }
}
